export default class Star {
    constructor(elem, num) {
        this.elem = $(elem);
        this.num = num;
        this.totalStar = 5;
        this.init();
    }

    init() {
        let innerContent = this.contructStar();
        this.elem.html(innerContent);
        this.setStar();
    }

    //构造星星
    contructStar() {
        let content = "", content1 = "", content2 = "";
        for (let i = 0; i < this.totalStar; i++) {
            let oneStar = "&#xe617";
            content1 += oneStar;
        }
        content1 = "<i class='star iconfont'>" + content1 + "</i>";

        for (let i = 0; i < this.totalStar; i++) {
            let oneStar = "&#xe618";
            content2 += oneStar;
        }

        content2 = "<i class='star iconfont'>" + content2 + "</i>";

        content = content1 + content2;

        return content;
    }

    //设置星级

    setStar() {
        let stars = [].slice.call(this.elem);
        stars.forEach(function (ele) {
            let starNum = ele.dataset.star;
            if (!isNaN(starNum)) {
                let percent = ((starNum / 5).toFixed(2)) * 100 + '%';
                $(ele).find('i')[1].style.width = percent;
            }
        })
    }

}